import sys
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
from common.helper import Helper
import execjs

class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "人人安康百万医疗保险-家庭版"
    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA": {
                # 年龄段
                "year": ["0-5", "6-10", "11-15", "16-20", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60"],
                # 是否有医保
                "socialSec": ["Y", "N"],
                # 保障期限
                "expire": ["1年"],
                # 交费类型
                "payType": ["年交"],
                # 一般医疗保险金
                "generalMedical": ["3000000"],
                # 恶性肿瘤保险金
                "cancerMedical": ["3000000"],
                # 家庭免赔额
                "deductible": ["20000"],
                # 人数范围
                "family": ["2-7"],
                # 赔付比例
                "payRate": ["100%"]
            },
            "planB":{
                # 年龄段
                "year": ["0-5", "6-10", "11-15", "16-20", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60"],
                # 是否有医保
                "socialSec": ["Y", "N"],
                # 保障期限
                "expire": ["1年"],
                # 交费类型
                "payType": ["年交"],
                # 一般医疗保险金
                "generalMedical": ["3000000"],
                # 恶性肿瘤保险金
                "cancerMedical": ["3000000"],
                # 家庭免赔额
                "deductible": ["10000"],
                # 人数范围
                "family": ["2-4"],
                # 赔付比例
                "payRate": ["100%"]

            }
        }
        for k in plan:
            for v in product( plan[k]["year"],plan[k]["socialSec"],plan[k]["expire"],plan[k]["payType"],plan[k]["generalMedical"],plan[k]["cancerMedical"],plan[k]["deductible"],plan[k]["family"],plan[k]["payRate"]):
                data = Helper.formatProductInfo( plan[k],v )
                self.mysql.cursor.execute( domain[self.name]["insert_sql"].format( **data ))
                self.mysql._conn.commit()
                print( domain[self.name]["insert_sql"].format( **data ) )
                
    # 执行爬虫
    def calcPremium( self,dt=None,mysql = None ):
        js = '''
            function CalculationFee(day,plan,index){
                var list = [
                    {
                        '0-4':[488,1078],
                        '5-10':[246,498],
                        '11-15':[148,268],
                        '16-20':[86,146],
                        '21-25':[146,298],
                        '26-30':[186,406],
                        '31-35':[256,576],
                        '36-40':[316,728],
                        '41-45':[406,1126],
                        '46-50':[598,1586],
                        '51-55':[766,2366],
                        '56-60':[976,3018],
                        '61-65':[1308,4058]
                    },
                    {
                        '0-4':[796,1780],
                        '5-10':[396,798],
                        '11-15':[236,436],
                        '16-20':[136,236],
                        '21-25':[238,496],
                        '26-30':[298,658],
                        '31-35':[406,908],
                        '36-40':[506,1206],
                        '41-45':[636,1836],
                        '46-50':[988,2618],
                        '51-55':[1258,3906],
                        '56-60':[1608,4990],
                        '61-65':[2165,6711]
                    }
                ];
                for (var key in list[plan]){
                    var keySplit = key.split("-");
                    if(day>=keySplit[0]&&day<=keySplit[1]){
                        price = list[plan][key][index].toFixed(2);
                        return price;
                    }
                }
            }
        '''
        day = int( dt["year"].split("-")[0])
        plan = 0 if dt["deductible"] == "20000" else 1
        index = 0 if dt["socialSec"] == "Y" else 1 
        cxt = execjs.compile( js )
        price = cxt.call("CalculationFee",day,plan,index)
        data  = {
            "name":self.name,
            "id":dt["id"],
            "price":price
        }
        print( data )
        self.mysql.updatePrice( data )
